import {Component} from '@angular/core';
import {AlertController, IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import {FavoriteListProvider} from "../../../providers/client/favorite-list/favorite-list.provider";
import {MessageBushService} from "../../../services/common/message-bush.service";
import {SideMenuService} from "../../../services/common/side-menu.service";
import {TabsPage} from "../../tabs/tabs";
import {LoadingService} from "../../../services/common/loading.service";

/**
 * Generated class for the FavoriteListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-favorite-list',
    templateUrl: 'favorite-list.html',
    providers: [
        FavoriteListProvider,
        SideMenuService
    ]
})
export class FavoriteListPage {

    favList: any;
    favCount: number = 0;
    favCountText: string = '共0个客户';
    isLoaded: boolean = false;

    openRoot = (name, index) => this.openTab(name, index);

    openMenu(myEvent) {
        this.sideMenuService.openSideMenu(myEvent)
    }

    openTab(name, index) {
        this.navCtrl.popToRoot()
        this.tabsPage.openPage({name: name, index: index});
    }

    constructor(public navCtrl: NavController,
                public favoriteProvider: FavoriteListProvider,
                public toastCtrl: ToastController,
                public alertCtrl: AlertController,
                public navParams: NavParams,
                private sideMenuService: SideMenuService,
                private loadingService: LoadingService,
                public tabsPage: TabsPage) {
    }

    ionViewDidLoad() {
        let status = MessageBushService.getMessage(MessageBushService.keys.favoriteIsUpdated);
        if (status == '0' || status == null) {
            this.getFavoriteList();
        }
    }

    ionViewDidEnter() {
        let status = MessageBushService.getMessage(MessageBushService.keys.favoriteIsUpdated);
        if (status === '1') {
            this.getFavoriteList();
            this.loadingService.dismissLoading();
        }
        MessageBushService.setMessage(MessageBushService.keys.favoriteIsUpdated, '0');
    }

    pullDownRefresh(refresher) {
        this.favoriteProvider.getFavoriteList().subscribe(res => {
            this.favList = res;
            if (this.favList) {
                this.favCount = this.favList.length;
            }
            this.favCountText = this.favCount > 0 ? '共'+this.favCount+'个客户' : '共0个客户';
            this.isLoaded = true;
            refresher.complete();
        }, error2 => {
            refresher.complete();
        });
    }

    getFavoriteList() {
        this.loadingService.showLoading();
        this.favoriteProvider.getFavoriteList().subscribe(res => {
            // console.log(res);
            this.favList = res;
            if (this.favList) {
                this.favCount = this.favList.length;
            }
            this.favCountText = this.favCount > 0 ? '共'+this.favCount+'个客户' : '共0个客户';

            this.isLoaded = true;

            this.loadingService.dismissLoading();
        }, error2 => {
            this.loadingService.dismissLoading();
        });
    }

    goFavoriteEdit() {
        // this.navCtrl.push('FavoriteEditPage');
        // this.navCtrl.push('FavoriteFilePage');
        this.navCtrl.push('InformationListPage');
    }

    // goFavoriteAdd() {
    //     this.navCtrl.push('ClientListPage');
    //     console.log('goFavoriteAdd');
    // }

    deleteFavorite(favoriteId: string) {
        // console.log('favoriteId is:' + favoriteId);
        this.alertCtrl.create({
            message: '确定要删除吗？',
            buttons: [
                {
                    text: '取消'
                },
                {
                    text: '确定',
                    handler: () => {
                        this.favoriteProvider.deleteFavorite(favoriteId).subscribe(res => {
                                this.getFavoriteList();
                            },
                            error2 => {
                                this.showToast('删除收藏失败！');
                            });
                    }
                }
            ]
        }).present();

    }

    goFavoriteDetail(clientNum: string, userType: string, clientRole: string) {
        if (userType == '1') {
            this.navCtrl.push('ClientDetailPage', {
                clientNum: clientNum,
                leads: true
            })
        } else if (userType == '2') {
            /*if (clientRole == 'O') {
                this.navCtrl.push('HolderClientDetailPage', {
                    clientNum: clientNum,
                    role: clientRole
                })
            } else if (clientRole == 'IO') {
                this.navCtrl.push('HolderClientDetailPage', {
                    clientNum: clientNum,
                    role: clientRole
                })
            } else {
                this.navCtrl.push('InsuredClientDetailPage', {
                    clientNum: clientNum,
                    role: clientRole
                })
            }*/
            this.navCtrl.push('HolderClientDetailPage', {
                clientNum: clientNum,
                role: clientRole
            })
        }
    }

    goBack() {
        this.navCtrl.pop();
    }

    showToast(msg: string) {
        this.toastCtrl.create({
            message: msg,
            position: 'middle',
            duration: 1200
        })
    }

    /**
     * vip 等级描述映射
     * @param level
     * @returns {string}
     */
    getDescribeByVipLevel(level) {
        let result = '';
        if (level == 1) {
            result = '黄金会员';
        } else if (level == 2) {
            result = '白金会员';
        } else if (level == 3) {
            result = '宏运世家钻石';
        } else if (level == 4) {
            result = '宏运世家至尊';
        }
        return result;
    }

}
